//package hzncc.wcs.config;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.diboot.notification.mapper.MessageMapper;
//
//import jakarta.annotation.PostConstruct;
//import jakarta.annotation.Resource;
//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//
//import org.springframework.amqp.core.*;
//import org.springframework.amqp.rabbit.connection.ConnectionFactory;
//import org.springframework.amqp.rabbit.connection.CorrelationData;
//import org.springframework.amqp.rabbit.core.RabbitAdmin;
//import org.springframework.amqp.rabbit.core.RabbitTemplate;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import java.nio.charset.Charset;
//
//import java.util.concurrent.LinkedBlockingQueue;
//
//@Configuration
//public class RabbitMQConfig {
//    private Log log = LogFactory.getLog(RabbitMQConfig.class);
//
//    @Resource
//    private ConnectionFactory connectionFactory;
//
//    @Resource
//    private MessageMapper messageMapper;
//
//    private LinkedBlockingQueue lbq = new LinkedBlockingQueue();
//
//
//    @PostConstruct
//    public void init() {
//
//        final String ROUTING_KEY = "plc";
//        final String EXCHANGE_NAME = "plc_exchange";
//        final String QUEUE = "plc_queue";
//
//        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
//        Queue queue = QueueBuilder.durable(QUEUE).build();
//        rabbitAdmin.declareQueue(queue);
//        Exchange exchange = ExchangeBuilder.directExchange(EXCHANGE_NAME).durable(true).build();
//        rabbitAdmin.declareExchange(exchange);
//
//        Binding binding = BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY).noargs();
//        rabbitAdmin.declareBinding(binding);
//    }
//
//    @Bean
//    public RabbitTemplate rabbitTemplate() {
//        RabbitTemplate rabbitTemplate = new RabbitTemplate();
//        rabbitTemplate.setConnectionFactory(connectionFactory);
//        rabbitTemplate.setMandatory(true);
//
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
////                String uniqueId = correlationData.getId();
//                if(ack) {
//                    log.info(String.format("消息到达交换机"));
//                }else {
//                    log.info(String.format("【消息未到达交换机】"));
//                }
//            }
//        });
//
//        // 先于 setConfirmCallback 先执行
////        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
////            String msg = new String(message.getBody(), Charset.defaultCharset());
////            String uniqueId = com.alibaba.fastjson.JSONObject.parseObject(msg).toJSONString();
////            log.warn(String.format("消息 {%s} 未到队列", uniqueId));
////            try {
////                lbq.put(uniqueId);
////            } catch (InterruptedException e) {
////                e.printStackTrace();
////            }
////        });
//
//        return rabbitTemplate;
//    }
//}
